;(function () {
  // 下拉
  $(function () {
    $('.c2-con-left')
      .on('mouseenter', function () {
        $(this).find('li').show()
      })
      .on('mouseleave', function () {
        $(this).find('li').hide()
      })

    // 侧边导航栏
    $('.content3')
      .on('mouseover', function () {
        $(this).find('.c3-back').stop(true, false).animate({ left: '0px' })
      })
      .on('mouseleave', function () {
        $(this).find('.c3-back').stop(true, false).animate({ left: '34px' })
      })

    // 联系客服
    $('.c3-con4')
      .on('mouseenter', function () {
        $(this).find('.c3-con4-show').show()
      })
      .on('mouseleave', function () {
        $(this).find('.c3-con4-show').hide()
      })

    // 扫码
    $('.c3-con5')
      .on('mouseenter', function () {
        $(this).find('.c3-con5-show').show()
      })
      .on('mouseleave', function () {
        $(this).find('.c3-con5-show').hide()
      })

    // 回到顶部
    $('.c3-con5').on('click', function () {
      $('html,body').animate({ scrollTop: 0 })
    })
    $('.top-feed').on('click', function () {
      $('html,body').animate({ scrollTop: 0 })
    })

    $('.hos')
      .on('mouseenter', function () {
        $(this).find('.sizeOver').stop().show()
        $(this).find('.toggIcon').stop().hide()
        $(this).find('.toggIcon2').stop().show()
      })
      .on('mouseleave', function () {
        $(this).find('.sizeOver').stop().hide()
        $(this).find('.toggIcon').stop().show()
        $(this).find('.toggIcon2').stop().hide()
      })
  })

  // 固定导航栏
  $(window).on('load', function () {
    let topH = $('.header').height() //获取到头部的高度
    $(window).on('scroll', function () {
      let sTop = $(this).scrollTop() //获取不可见区域的高度
      if (sTop > topH) {
        $('.content2').css({
          backgroundColor: 'rgba(0,0,0,0)',
          position: 'fixed',
          top: 0
        })
        $('.buttom').css({
          marginTop: $('.c2-con').height()
        })
      } else {
        $('.content2').css({
          backgroundColor: 'rgba(0,0,0,1)',
          position: 'static'
        })
        $('.buttom').css({
          marginTop: '20px'
        })
      }
    })
  })

  // // 图片轮播
  class Swiper {
    constructor(el) {
      this.$el = el
      this.$oUl = this.$el.find('ul')
      this.$oOl = this.$el.find('ol')
      this.imgIndex = 0
      this.tempIndex = 0
      this.init()
    }
    //   初始化
    init() {
      //   1、把第一张图片克隆到最后位置
      let $cloneLi = this.$el.children('.screen').find('li').first().clone(true)
      $cloneLi.appendTo(this.$oUl)
      // 2、下面的li要动态创建
      for (var i = 0; i < this.$oUl.children().length; i++) {
        let $li = $(`<li>${i + 1}</li>`)
        $li.addClass(i == 0 ? 'current' : '')
        $li.appendTo(this.$oOl)
      }
      this.timer = setInterval(() => {
        this.imgIndex++
        this.tempIndex++
        this.autoPlay()
      }, 3000)
    }

    //   自动播放
    autoPlay() {
      if (this.imgIndex > 2) {
        this.imgIndex = 1
        this.$oUl.css({ left: 0 })
      }
      this.$oUl.animate({ left: this.imgIndex * -210 }, 1500)
      if (this.tempIndex >= 2) {
        this.tempIndex = 0
      }
      this.$oOl.children().eq(this.tempIndex).addClass('current').siblings().removeClass('current')
    }
  }

  $.fn.extend({
    Swiper() {
      new Swiper(this)
    }
  })

  $(function () {
    $('.c1-con3').Swiper()
  })
})()
;(function () {
  // 登录
  window.onload = function () {
    var oForm1 = document.querySelector('.userloginform')
    console.log(oForm1)
    var oMsg1 = document.querySelector('.msg')
    // 用户名默认未注册过
    oForm1.onsubmit = function () {
      let name = oForm1.name.value
      console.log(name)
      let pwd = oForm1.pwd.value
      if (name == '' || pwd == '') {
        oMsg1.innerHTML = '提示：用户名不能为空'
        return false
      } else {
        oMsg1.innerHTML = ''
      }

      var oUser = {
        name: this.name.value,
        pwd: this.pwd.value
      }

      var isLogin = false //默认不能登录
      var oUserInfo = {}
      var oUserList = JSON.parse(localStorage.getItem('userInfo') || '[]')
      for (var i = 0; i < oUserList.length; i++) {
        if (oUserList[i].name === oUser.name && oUserList[i].pwd === oUser.pwd) {
          isLogin = true
          oUserInfo = oUserList[i]
          break
        }
      }

      if (oUserList.name != undefined) {
        oForm1.name.value = oUserList.name
        oForm1.pwd.value = oUserList.pwd
      }

      if (isLogin) {
        // 把成功登录人的信息,保存一份,在sessionStorage中
        sessionStorage.setItem('user', JSON.stringify(oUserInfo))
        alert('登陆成功')
        location = '../html/cart.html'
      } else {
        alert('登录失败')
      }

      return false
    }

    // 注册
    var oForm = document.querySelector('.registerform')
    console.log(oForm)
    var oPass = document.querySelector('.pass')
    var oMsg = document.querySelector('.registMsg')
    var userFlag = false
    oForm.name.oninput = function () {
      let name = this.value.trim()
      console.log(name)
      //用户名只包含数字,字母,下划线, 且长度不小于6位
      var reg = /^\w{5,15}$/i
      userFlag = reg.test(name)
      if (userFlag) {
        oMsg.innerHTML = ''
        //1.获取本地数据,返回是数组
        var oUserList = JSON.parse(localStorage.getItem('userInfo') || '[]')
        for (var i = 0; i < oUserList.length; i++) {
          if (oUserList[i].name == name) {
            userFlag = false
            break
          }
        }
        if (userFlag == true) {
          oMsg.innerHTML = ''
        } else {
          oMsg.innerHTML = '提示：该用户名已经被注册,请重新输入'
        }
      } else {
        oMsg.innerHTML = '提示：用户名只包含数字,字母,下划线, 且长度不小于6位'
      }
    }

    // 密码
    var pwdFlag = false
    oForm.pwd.onblur = function () {
      var reg = /^.{8,16}$/i
      pwdFlag = reg.test(this.value)
      if (pwdFlag) {
        oMsg.innerHTML = ''
      } else {
        oMsg.innerHTML = '提示：密码长度在8到16位'
      }
    }

    //判断2次密码是否一致
    var rpwdFlag = false
    oForm.pwd2.onblur = function () {
      var oRpwd = this.value
      console.log(oRpwd)
      var oPwd = oForm.pwd.value
      if (oRpwd == oPwd) {
        rpwdFlag = true
        oMsg.innerHTML = ''
      } else {
        oMsg.innerHTML = '提示：密码不一致'
      }
    }

    //校验手机
    var phoneFlag = false
    oForm.telephone.onblur = function () {
      var phone = oForm.telephone.value
      var reg = /^[1][3-9]\d{9}$/
      phoneFlag = reg.test(phone)
      if (phoneFlag) {
        oMsg.innerHTML = ''
      } else {
        oMsg.innerHTML = '提示：手机号不合法'
      }
    }

    // 随机验证码
    function randomNum(n, m) {
      var min = n < m ? n : m //找到最大的
      var max = n > m ? n : m //找到最小的
      //num  0 ~ 25
      var num = parseInt((max - min + 1) * Math.random())
      return min + num
    }
    // randomNum(65, 90)
    //根据数值转成字符
    function randomStr(n, m) {
      var num = randomNum(n, m)
      return String.fromCharCode(num)
    }

    // console.log(randomStr(97, 122));

    //随机生成验证码 (大写,小写,数值)

    function randomCode(n) {
      var str = ''
      for (var i = 0; i < n; i++) {
        var number = Math.random() //随机 0~9
        if (number >= 0 && number < 0.4) {
          str += randomStr(65, 90) //大写字母
        } else if (number > 0.4 && number < 0.6) {
          str += randomStr(97, 122) //小写字母
        } else {
          str += randomNum(0, 9)
        }
      }
      return str
    }
    oPass.innerHTML = randomCode(4)
    oPass.onclick = function () {
      this.innerHTML = randomCode(4)
    }

    // 验证码验证
    var codeFlag = false
    oForm.code.onblur = function () {
      var code = oForm.code.value
      var pass = oPass.innerHTML
      if (code == pass) {
        codeFlag = true
        oMsg.innerHTML = ''
      } else {
        oMsg.innerHTML = '提示：验证码不相符'
      }
    }

    // 确认是否勾选
    var agreementFlag = false
    oForm.agreement.onblur = function () {
      let checked = oForm.agreement.checked
      if (checked) {
        agreementFlag = true
        oMsg.innerHTML = ''
      } else {
        oMsg.innerHTML = '提示：请确认阅读特步用户协议'
      }
    }

    // 提交
    oForm.onsubmit = function () {
      if (!(userFlag && pwdFlag && rpwdFlag && phoneFlag && codeFlag)) {
        alert('数据不合法')
        return false
      }
      //1.获取本地数据 数组
      var oUserList = JSON.parse(localStorage.getItem('userInfo') || '[]')
      //2.得到输入的内容,做成一个对象
      var oUser = {
        name: this.name.value,
        pwd: this.pwd.value,
        telephone: this.telephone.value
      }

      var isAdd = true //默认没有注册过
      for (var i = 0; i < oUserList.length; i++) {
        if (oUserList[i].name == oUser.name) {
          isAdd = false
          break
        }
      }
      if (isAdd) {
        oUserList.push(oUser)
        localStorage.setItem('userInfo', JSON.stringify(oUserList))
        alert('注册成功')
        location = '../html/login.html'
      } else {
        alert('注册失败')
      }
      return false
    }
  }
})()
